from transformers import BertTokenizer, BertForSequenceClassification
import torch

# 指定你微调好的模型名称（该模型应已针对情感分析进行训练）
model_name = "ckiplab/bert-base-chinese"  # 请替换为你自己的模型名称
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)  # 2分类：正面和负面

def predict_sentiment(text):
    # 对文本进行编码
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    # 模型前向推理
    outputs = model(**inputs)
    logits = outputs.logits
    # 取预测分数最高的类别
    sentiment = torch.argmax(logits).item()
    return "正面情感 😊" if sentiment == 1 else "负面情感 😠"

# 示例文本
text1 = "这款手机真的很好，我很喜欢！"
text2 = "质量太差了，太让我失望了！"
text3 = "你是傻逼吗，没看到我在干活吗"

print("Text1:", predict_sentiment(text1))
print("Text2:", predict_sentiment(text2))
print("Text3:", predict_sentiment(text3))
